package com.example.androidstart;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;

import com.example.androidstart.coroutines.CoroutineLearnActivity;
import com.example.moduleone.InternalA;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: InternalA.TAG "+InternalA.TAG);
        // 测试push
        findViewById(R.id.startConstantLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ConstantActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.simpleHomePage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SimpleHomePageActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        findViewById(R.id.openglES).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, openglesActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.androidAppBundle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AppBundleBaseActivity.class);
                MainActivity.this.startActivity(intent);
                Log.d(TAG, "onClick: androidAppBundle");
            }
        });
        findViewById(R.id.motionevent).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MotionEventDistributeActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        findViewById(R.id.testfilereadspeedAndMemory).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, TestFileReadSpeed.class);
                MainActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.arcoreRenderCameraBg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ArCoreActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        findViewById(R.id.imageBigger).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, BiggerImageActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.ScreenOrientationChange).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ScreenOrientation.class);
                MainActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.testActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.GraphicActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, GraphicActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });
        findViewById(R.id.CoroutineActivity).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CoroutineLearnActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });
        StackTraceElement[] stackTraceElement = new Throwable().getStackTrace();
        Log.i(TAG, "onCreate: " + stackTraceElement.length);
        Log.i(TAG, "onCreate: Arrays.toString(stackTraceElement)" + stackTraceElement[1].getMethodName());
        android.util.Log.i(TAG, "   stacktrace  " + android.util.Log.getStackTraceString(new Throwable()));


        String str = "liuxudong";
        byte[] arr = Base64.encode(str.getBytes(), Base64.NO_WRAP);
        Log.d(TAG, "onCreate: " + Arrays.toString(arr));
        String decodeStr = new String(Base64.decode(arr, Base64.NO_WRAP), StandardCharsets.UTF_8);
        Log.d(TAG, "onCreate: decode is " + decodeStr);

        File cacheFile = new File(MainActivity.this.getCacheDir() + File.separator + "liuxudong");

//        Log.i(TAG, "onCreate: res is is str1 " + Integer.toHexString(R.string.string1));
//        Log.i(TAG, "onCreate: res is is str2 " + Integer.toHexString(R.string.string2));
//        Log.i(TAG, "onCreate: res is is str3 " + Integer.toHexString(R.string.string3));
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    private void writeFile(File file, String value) {
        ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        try (FileOutputStream outputStream = new FileOutputStream(file, true);
             FileChannel fileChannel = outputStream.getChannel();) {
            Properties properties = new Properties();
            properties.setProperty("MD5", value);
            properties.store(outputStream, "write key");
        } catch (IOException e) {
            Log.w(TAG, "writeFile: e is " + e);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }
}